﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication

Partial Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"
        Dim myFeed As New SyndicationFeed
        myFeed.Title = TextSyndicationContent.CreatePlaintextContent("Nigerian Traditional Styles")

        myFeed.Description = TextSyndicationContent.CreatePlaintextContent("The 10 Most recent Styles added to NaijaTradStyles website")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink(New Uri(okURL("StyleDetails.aspx"))))
        myFeed.Language = "en-us"
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT TOP 10 * from UsersUpload ORDER BY [ID] DESC"
        'WHERE ID =(SELECT MAX(ID) FROM UsersUpload)"
        'Dim cmdQuery As String = "SELECT TOP 5 * FROM UsersUpload WHERE ID IN (SELECT ID FROM UsersUpload WHERE ID <= MAX(ID)) MINUS (SELECT ID FROM UsersUpload WHERE ID <= MAX(ID)-5) "

        Dim dbComm = New OleDbCommand(cmdQuery, oleDbConn)


        Dim dbRead = dbComm.ExecuteReader()
        Dim myItems As New List(Of SyndicationItem)

        While dbRead.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = mkRssTxt(dbRead("PicTitle"))
            sItem.Summary = mkRssTxt(dbRead("Description"))
            ' sItem.Links.Add(mkRssLink(dbRead("imageURL")))
            myItems.Add(sItem)

        End While
        myFeed.Items = myItems
        Dim feedwriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
        Dim rssformatter = New Rss20FeedFormatter(myFeed)
        rssformatter.WriteTo(feedwriter)
        feedwriter.Close()


    End Sub
    'Private Function mkRssLink(ByVal someLocation As String) As SyndicationLink
    '   Return SyndicationLink.CreateAlternateLink(New Uri(someLocation))
    ' End Function
    Private Function mkRssTxt(ByVal somestring As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(somestring)
    End Function

    Private Function okURL(ByVal urlstr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlstr)

    End Function

End Class